package com.ynr.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

public class BaseController {

	private static final Logger logger = LoggerFactory
			.getLogger(BaseController.class);
	
	protected CommonResult validParams(BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            FieldError fieldError = bindingResult.getFieldError();
            return processBindingError(fieldError);
        }
        return ResultUtil.returnSuccess(CodeInfoEnum.PARAM_SUCCESS);
    }

	private CommonResult processBindingError(FieldError fieldError) {
        String code = fieldError.getCode();
        logger.debug("validator error code: {}", code);
        switch (code) {
            case "NotEmpty":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_EMPTY);
            case "NotBlank":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_EMPTY);
            case "NotNull":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_EMPTY);
            case "Pattern":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_PATTERN_ERROR);
            case "Min":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_MIN_ERROR);
            case "Max":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_MAX_ERROR);
            case "Length":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_LENGTH_ERROR);
            case "Range":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_RANGE_ERROR);
            case "Email":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_EMAIL_ERROR);
            case "DecimalMin":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_DECIMALMIN_ERROR);
            case "DecimalMax":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_DECIMALMAX_ERROR);
            case "Size":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_SIZE_ERROR);
            case "Digits":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_DIGITS_ERROR);
            case "Past":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_PAST_ERROR);
            case "Future":
                return ResultUtil.returnError(CodeInfoEnum.PARAM_FUTURE_ERROR);
            default:
                return ResultUtil.returnError(CodeInfoEnum.PARAM_UNKNOWN_ERROR);
        }
    }
}